package com.timemaneger.thinkintime;

import android.os.Environment;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.timemaneger.thinkintime.R;

import com.timemaneger.thinkintime.GridView.BackgroundPreview;

import java.io.File;

public class AddBookActivity extends AppCompatActivity {
    private int []backgrounddata={R.mipmap.book1,R.mipmap.book2,R.mipmap.book3,R.mipmap.book4,R.mipmap.book5};
    private String []hints={"用心过生活","敲下零碎的日子","执子之手我们到老","我的背包和征程","平行时光"};
    private long cover=R.mipmap.book1;
    private EditText book_title;
    private BackgroundPreview backgroundPreview;
    private ImageView book_cover;
    private Button cancle;
    private Button save;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_book);
        init();
    }

    private void init() {
        book_title=(EditText) findViewById(R.id.book_title);
        book_cover =(ImageView) findViewById(R.id.book_cover);
        backgroundPreview=(BackgroundPreview) findViewById(R.id.book_background);
        BackGroundAdapter backGroundAdapter=new BackGroundAdapter();
        backgroundPreview.setAdapter(backGroundAdapter);
        backgroundPreview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                if(position>=backgrounddata.length){

                }else{
                    Handler handler=new Handler();
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            book_cover.setBackgroundResource(backgrounddata[position]);
                            book_title.setHint(hints[position]);
                            cover=backgrounddata[position];
                        }
                    });

                }
            }
        });
        cancle=(Button) findViewById(R.id.cancle);
        cancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
        save=(Button) findViewById(R.id.save);
        save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title=book_title.getText().toString();
                if(title.equals("")){
                    title=book_title.getHint().toString();
                }
                String path= Environment.getExternalStorageDirectory().getAbsolutePath()+"/Html/"+title+"-"+cover;
                try{
                    File dir=new File(path);
                    if(dir.exists()){
                        Toast.makeText(getApplicationContext(),title+"书册已存在",Toast.LENGTH_SHORT).show();
                    }else {
                        dir.mkdir();
                        Toast.makeText(getApplicationContext(),title+"书册创建成功",Toast.LENGTH_SHORT).show();

                        setResult(RESULT_OK);
                        finish();
                    }
                }catch (Exception e){
                    Toast.makeText(getApplicationContext(),title+"书册创建失败",Toast.LENGTH_SHORT).show();
                    return;
                }
            }
        });
    }

    class BackGroundAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return backgrounddata.length;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView= LayoutInflater.from(getApplicationContext()).inflate(R.layout.background_item,null);
            TextView view=(TextView) convertView.findViewById(R.id.background_item_title);
            if(backgrounddata.length>position){
                view.setBackgroundResource(backgrounddata[position]);
                view.setText("");
            }else {
                view.setBackgroundResource(backgrounddata[0]);
                view.setClickable(false);
                view.setVisibility(View.INVISIBLE);
            }
            return convertView;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public Object getItem(int position) {
            return position;
        }
    }
}
